package com.halflife.concurrence;

public class WaitTest {
    public static int i = 1;
    public static int j = 0;
    public static synchronized void waitTest() {
        System.out.println(Thread.currentThread().getName()+":"+i);
        if (i%10==0) {
            try {
                i++;
                j=1;
                WaitTest.class.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else if (j==1) {
            j=0;
            WaitTest.class.notifyAll();
        }
        if (i > 2000) {
            System.exit(0);
        }
        i++;
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(()->{
            while (true) {
                WaitTest.waitTest();
            }

        },"thread1");
        Thread thread2 = new Thread(()->{
            while (true) {
                WaitTest.waitTest();
            }
        },"thread2");
        thread1.start();
        thread2.start();
        while (true) {

        }

    }
}
